import java.util.Random;

import javax.media.opengl.GL;


public class SquareCut extends PuzzleCut{
	double size;
	float displacement;

	public SquareCut()
	{
		Random rand = new Random();
		//between PuzzlePiece.size/10
		//and PuzzlePiece.size*.3
		double lower = PuzzlePiece.size/6;
		double upper = PuzzlePiece.size*.3;
		size = rand.nextDouble()*(upper - lower)+lower;
		displacement=(float) (rand.nextDouble()*(size/2));
		//randomize invert
		double inv = rand.nextDouble();
		inverted = true;
		if (inv>=.5)
			inverted = false;
	}
	public SquareCut(double size, float displacement, boolean inverted)
	{
		this.size = size;
		this.displacement = displacement;
		this.inverted = inverted;
	}
	/**
	 * the color of the piece and the background color have to be specified,
	 * the piece will know which one to use by the way it was setup.
	 * @param gl
	 * @param color
	 * @param backColor
	 * @param Coordinate location of cut
	 * @param Coordinate location of piece
	 */
	public void display(GL gl,PuzzleColor color,PuzzleColor backColor,Coordinate coord,Coordinate location,int direction)
	{
		float disx = 0;
		float disy =0;
		if(inverted)
		{
			switch(direction){
			case 0:disy = 0 - displacement;break;
			case 1:disy = displacement;break;
			case 2:disx = displacement;break;
			case 3:disx = 0- displacement;break;
			}
			gl.glColor3f(backColor.getRed(), backColor.getGreen(), backColor.getBlue());
		}
		else
		{
			switch(direction){
			case 0:disy = displacement;break;
			case 1:disy = 0 - displacement;break;
			case 2:disx = 0 - displacement;break;
			case 3:disx = displacement;break;
			}
			gl.glColor3f(color.getRed(), color.getGreen(), color.getBlue());
		}
		gl.glBegin (GL.GL_POLYGON);
		float x = coord.x+disx;
		float y = coord.y+disy;
		if(!inverted)
		{
			gl.glVertex2f((float)(x-size),(float)(y-size));
			gl.glVertex2f((float)(x+size),(float)(y-size));
			gl.glVertex2f((float)(x+size),(float)(y+size));
			gl.glVertex2f((float)(x-size),(float)(y+size));
		}
		else
		{
			double minX =location.x;
			double minY = location.y;
			double maxX = location.x+PuzzlePiece.size;
			double maxY = location.y+PuzzlePiece.size;
			float x1 =(float)(x-size);
			float y1 =(float)(y-size);
			float x2 =(float)(x+size);
			float y2 =(float)(y+size);
			if(x1<minX)
				x1 = (float)minX;
			else if(x1>maxX)
				x1=(float)maxX;
			if(y1<minY)
				y1 = (float)minY;
			else if(y1>maxY)
				y1=(float)maxY;
			if(x2<minX)
				x2 = (float)minX;
			else if(x2>maxX)
				x2=(float)maxX;
			if(y2<minY)
				y2 = (float)minY;
			else if(y2>maxY)
				y2=(float)maxY;
			gl.glVertex2f(x1,y1);
			gl.glVertex2f(x2,y1);
			gl.glVertex2f(x2,y2);
			gl.glVertex2f(x1,y2);
		}
		gl.glEnd();
	}
	/**
	 * a clone method that clones the same shape in a different spot
	 * the position is determined by coord coordinate
	 * @param invert
	 * @return
	 */
	public PuzzleCut clone(boolean bool)
	{
		PuzzleCut result =new SquareCut(size, displacement,inverted);
		if(bool)
				result.invert();

		return result;
	}
}
